package cn.tjjcy.service;

import cn.tjjcy.common.DmMsg;
import cn.tjjcy.common.GzResult;
import cn.tjjcy.dao.RealGzLogMapper;
import cn.tjjcy.domain.RealGzConfEntity;
import cn.tjjcy.domain.RealGzLogEntity;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class YwscGzLogService {

    @Autowired
    private RealGzLogMapper realGzLogMapper;


    public void initLog(String gzLogId, GzResult gzResult, DmMsg dmMsg, RealGzConfEntity realGzConfEntity) {
        RealGzLogEntity logEntity = new RealGzLogEntity();
        logEntity.setId(gzLogId);
        logEntity.setBmsah(gzResult.getBmsah());
        logEntity.setGzSql(gzResult.getGzSql());
        logEntity.setGzErrorReason(gzResult.getGzErrorReason());
        logEntity.setGzConfId(realGzConfEntity.getId());
        logEntity.setTableName(realGzConfEntity.getTableName());
        logEntity.setOpType(realGzConfEntity.getOpType());
        logEntity.setDmMsg(JSON.toJSONString(dmMsg));
        logEntity.setCreateTime(new Date());
        realGzLogMapper.insert(logEntity);
    }

    public void update(String logId, GzResult gzResult) {
        RealGzLogEntity logEntity = new RealGzLogEntity();
        logEntity.setResultSql(gzResult.getResultSql());
        logEntity.setResult(String.valueOf(gzResult.getResult()));
        logEntity.setBmsah(gzResult.getBmsah());

        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", logId);
        realGzLogMapper.update(logEntity, updateWrapper);
    }

}
